package com.joeyzh.base.filter;

import android.content.Context;
import android.support.annotation.IdRes;

import com.joeyzh.base.IPresenter;
import com.joeyzh.base.IViewModel;
import com.joeyzh.base.PresenterDelegate;

import org.w3c.dom.Entity;

import java.util.HashMap;

/**
 * Created by Joey on 2018/7/19.
 * 搜索的Filter的一个抽象封装
 *
 * @author Joey
 */

public abstract class BaseFilterPresenter implements PresenterDelegate {


    private FilterResultInterface resultInterface;

    /**
     * 用于存放扫描的键值对
     */
    protected HashMap<String, String> filterResult = new HashMap<>();

    /**
     * 用于存放提交的键值对清单
     */
    protected HashMap<Integer, String> searchKeys = new HashMap<>();
    /**
     * 存放基础数据源列表
     */
    protected HashMap<String, Object> baseMap = new HashMap<>();

    public BaseFilterPresenter(Context context) {
        initSearchInfo();
        if (context instanceof FilterResultInterface) {
            setResultInterface((FilterResultInterface) context);
        }
    }

    /**
     * 搜索结果回调
     *
     * @param resultInterface
     */
    public void setResultInterface(FilterResultInterface resultInterface) {
        this.resultInterface = resultInterface;
    }

    @Override
    public void submit() {
        if (!checkoutInfo()) {
            return;
        }
        if (resultInterface != null) {
            resultInterface.filterResult(filterResult, baseMap);
        }
    }

    /**
     * 将键值对与搜索条件绑定
     *
     * @param resId
     * @param key
     */
    public void bindSearchKeys(int resId, String key, String defValue) {
        searchKeys.put(resId, key);
        addResult(resId, defValue);
    }

    /**
     * 清除搜索结果
     */
    public void clear() {
        filterResult.clear();
    }

    /**
     * 添加搜索结果
     *
     * @param resId
     * @param value
     */
    public void addResult(int resId, String value) {
        String key = searchKeys.get(resId);
        if (key == null) {
            return;
        }
        filterResult.put(key, value);
    }

    /**
     * 判断是否存在
     *
     * @param resId
     * @return
     */
    public boolean hasResult(int resId) {
        String key = searchKeys.get(resId);
        if (key == null) {
            return false;
        }
        String value = filterResult.get(key);
        if (value == null || value.isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * 添加数据源
     *
     * @param key
     * @param value
     */
    public void addBaseInfo(String key, Object value) {
        baseMap.put(key, value);
    }

    /**
     * 初始化搜索的基础数据
     */
    public abstract void initSearchInfo();


}
